//
//  PhotoCollectionViewCell.swift
//  PhotoTestDemo
//
//  Created by 惠 on 2020/9/8.
//  Copyright © 2020 惠. All rights reserved.
//

import UIKit

class PhotoCollectionViewCell: UICollectionViewCell {
    
    var colorType: ColorType! {
        didSet {
            switch colorType {
            case .defaults:
                selectButton.setImage(UIImage(named: "selectedImage", in: Resources.getBundle, compatibleWith: nil), for: .selected)
            case .red:
                selectButton.setImage(UIImage(named: "redSelectedImage", in: Resources.getBundle, compatibleWith: nil), for: .selected)
            case .none:
                selectButton.setImage(UIImage(named: "selectedImage", in: Resources.getBundle, compatibleWith: nil), for: .selected)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
//MARK: -懒加载UI
    lazy var photoImage: UIImageView = {
        let photoImage = UIImageView(frame: self.bounds)
        photoImage.contentMode = .scaleAspectFill
        photoImage.clipsToBounds = true
        return photoImage
    }()
    
    lazy var selectButton: UIButton = {
        let selectButton = UIButton(type: .custom)
        selectButton.frame = CGRect(origin: CGPoint(x: self.bounds.width-30, y: 0), size: CGSize(width: 30, height: 30))
        selectButton.setImage(UIImage(named: "notSelectImage", in: Resources.getBundle, compatibleWith: nil), for: .normal)
        return selectButton
    }()

    lazy var timeLabel: UILabel = {
        let timeLabel = UILabel(frame: CGRect(origin: CGPoint(x: 0, y: self.bounds.size.height-20), size: CGSize(width: self.bounds.size.width-2, height: 20)))
        timeLabel.textColor = .white
        timeLabel.textAlignment = .right
        timeLabel.font = UIFont.systemFont(ofSize: 12)
        timeLabel.isHidden = true
        return timeLabel
    }()
    
//MARK: -设置UI
    func setUI() {
        backgroundColor = .white
        
        addSubview(photoImage)
        addSubview(selectButton)
        addSubview(timeLabel)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let btnPoint = self.convert(point, to: self.selectButton)
        if self.selectButton.point(inside: btnPoint, with: event) {
            return self.selectButton
        }
        return super.hitTest(point, with: event)
    }
}
